package com.cms.server.controller;

import com.cms.server.Entity.ContractEntity;
import com.cms.server.Entity.UserIDEntity;
import com.cms.server.mapper.*;
import com.cms.server.model.Contract;
import com.cms.server.model.Contract_state;
import com.cms.server.tool.Global;
import com.cms.server.tool.Timestamp;
import com.cms.server.tool.sendMessage;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/contract")
public class ContractController {
    @Resource
    private ContractMapper contractMapper;
    @Resource
    private ContractProcessMapper contractProcessMapper;
    @Resource
    private ContractStateMapper contractStateMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ContractAttachmentMapper contractAttachmentMapper;

    @PostMapping("/addNew")
    public ResponseEntity<Map<String, Object>> addNew(@RequestParam(value = "userid", required = true) String userid){
        Integer user_id = Integer.parseInt(userid);
        Contract contract = new Contract();
        contract.setBeginTime(Timestamp.now_time());
        contract.setEndTime(0);
        contract.setDel(0);
        contract.setCustomer("");
        contract.setName("");
        contract.setNum(0);
        contract.setUser_id(user_id);
        contract.setType(1);
        contractMapper.insert(contract);

        Map<String,Object> response = new HashMap<>();
        Map<String,Object> mes = new HashMap<>();
        mes.put("con_id", contract.getId());
        response.put("data", mes);
        response.put("status",true);

        return ResponseEntity.ok(response);
    }

    // 起草合同
    @PostMapping("/addContract")
    public ResponseEntity<Map<String, String>> addContract(@RequestBody ContractEntity contractEntity){
        Contract contract = new Contract();
        contract.setBeginTime(Timestamp.now_time());
        contract.setEndTime(0);
        contract.setDel(0);
        contract.setCustomer(contractEntity.getCustomer());
        contract.setName(contractEntity.getName());
        contract.setNum(contractEntity.getNum());
        contract.setUser_id(Global.user_id);
        contract.setType(1);
        contractMapper.insert(contract);

        // 增加合同状态
        Contract_state contractState = new Contract_state();
        contractState.setType(1); // 起草
        contractState.setDel(0);
        contractState.setCon_id(contract.getId());
        contractState.setTime(Timestamp.now_time());
        contractStateMapper.insert(contractState);

        return ResponseEntity.ok(sendMessage.createResponse(true,"合同创建成功"));
    }

    // 返回本用户所有合同信息
    @PostMapping("/info")
    public ResponseEntity<Map<String,Object>> info(@RequestBody UserIDEntity userid){
        // 获取当前用户ID
        Integer user_id = userid.getUser_id();

        // 查询当前用户的所有合同
        List<Contract> contracts = contractMapper.selectById(user_id);
        List<Map<String,Object>> data = new ArrayList<>();
        int num[] = {0,0,0,0,0}; // 统计各个合同的数量
        // 如果查询结果不为空
        if (!contracts.isEmpty()) {
            // 将合同列表转换为响应格式
            for(Contract contract : contracts){
                Map<String,Object> temp = new HashMap<>();
                temp.put("id",contract.getId());
                temp.put("name",contract.getName());
                temp.put("beginTime",Timestamp.stamp_to_data(contract.getBeginTime()));
                temp.put("num",contract.getNum());
                temp.put("endTime",Timestamp.stamp_to_data(contract.getEndTime()));
                temp.put("customer",contract.getCustomer());
                temp.put("contract",contract.getContent());
                temp.put("type",contract.getType());
                data.add(temp);
                num[contract.getType()-1]++;
            }
        }
        Map<String,Object> response = new HashMap<>();
        response.put("status",true);
        response.put("data", data);
        response.put("user_id",Global.user_id);
        for(int i = 0; i < 5; i++){
            response.put(""+(i+1), num[i]);
        }
        return ResponseEntity.ok(response);
    }

    // 获得工作进程
    @PostMapping("/workList")
    public ResponseEntity<Map<String,Object>> getWorkList(@RequestBody UserIDEntity userid){
        // 获取当前用户ID
        Integer user_id = userid.getUser_id();

        // 查询当前用户的所有合同
        List<Contract> contracts = contractMapper.selectById(user_id);
        Map<String ,Object> data = new HashMap<>();
        List<Map<String,Object>> List = new ArrayList<>();
        List<Map<String,Object>> c1 = new ArrayList<>(); // 待处理
        List<Map<String,Object>> c2 = new ArrayList<>(); // 处理中
        List<Map<String,Object>> c3 = new ArrayList<>(); // 已完成
        // 如果查询结果不为空
        if (!contracts.isEmpty()) {
            // 将合同列表转换为响应格式
            for(Contract contract : contracts){
                Map<String,Object> temp = new HashMap<>();
                temp.put("name",contract.getName());
                temp.put("id",contract.getId());
                temp.put("opting","类型："+ Global.intToType(contract.getType()));
                temp.put("tags","新增");
                switch (contract.getType()){
                    case 1 : case 3 :case 4 :
                        c1.add(temp);
                        break;
                    case 2:
                        c2.add(temp);
                        break;
                    case 5:
                        c3.add(temp);
                        break;
                }
            }
        }
        Map<String,Object> one = new HashMap<>();
        Map<String,Object> two = new HashMap<>();
        Map<String,Object> three = new HashMap<>();
        one.put("name","待处理");
        one.put("children",c1);
        two.put("name","处理中");
        two.put("children",c2);
        three.put("name","已完成");
        three.put("children",c3);
        List.add(one);
        List.add(two);
        List.add(three);
        data.put("list", List);
        Map<String,Object> response = new HashMap<>();
        response.put("code",200);
        response.put("data", data);
        response.put("msg","");
        return ResponseEntity.ok(response);
    }

    // 返回本用户所有合同信息
    @PostMapping("/deleteNoUse")
    public ResponseEntity<Map<String,Object>> deleteNo(@RequestParam(value = "userid", required = true) String userid){
        contractMapper.deleteNoName(Integer.parseInt(userid));
        return ResponseEntity.ok(sendMessage.createResponse(true,"success"));
    }
}


